//
//  SN_Question_ResolveController.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/21.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_Question_ResolveController.h"

#import "SN_Question_Solve_Model.h"
#import "SN_Question_SolveList_Model.h"
#import "SN_ResoveQuestion_SectionHeaderView.h"
#import "SN_ResoQuestion_TableViewCell.h"

#import "SN_Question_Detail_Controller.h"

@interface SN_Question_ResolveController ()<UITableViewDelegate,UITableViewDataSource,SN_ResoQuestion_TableViewCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UIView *tableHeaderView;

@property (nonatomic, strong) UIImageView *newsImageView;
@property (nonatomic, strong) UILabel *contentLabel; //内容
@property (nonatomic, strong) UILabel *yearLabel; //年度
@property (nonatomic, strong) UILabel *courseLabel; //科目和期数

@end

@implementation SN_Question_ResolveController {
    
    NSArray *chineseArr;
}



#pragma mark -Getter
- (UITableView *)tableView {
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight) style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.tableHeaderView;
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
    }
    
    return  _dataArr;
}



- (UIView *)tableHeaderView {
    
    if (!_tableHeaderView) {
        
        _tableHeaderView = [[UIView alloc] init];
        _tableHeaderView.frame = CGRectMake(0, 0, VIEW_WIDTH, 132);
        _tableHeaderView.backgroundColor = [UIColor whiteColor];
        _newsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 16, 75, 100)];
        _newsImageView.contentMode = UIViewContentModeScaleToFill;
        _newsImageView.clipsToBounds = YES;
        [_tableHeaderView addSubview:_newsImageView];
        
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_newsImageView.frame) + 14, 28, 200, 16)];
        _contentLabel.font = [UIFont systemFontOfSize:16];
        _contentLabel.text = @"";
        
        _contentLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        [_tableHeaderView addSubview:_contentLabel];
        
        _yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_newsImageView.frame) + 14, 56, 200, 14)];
        _yearLabel.font = [UIFont systemFontOfSize:14];
        _yearLabel.text = @"";
        _yearLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        [_tableHeaderView addSubview:_yearLabel];
        
        _courseLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_newsImageView.frame) + 14, 90, 200, 15)];
        _courseLabel.font = [UIFont systemFontOfSize:15];
        _courseLabel.text = @"";
        _courseLabel.textColor = [UIColor colorWithHexString:@"#fb524a"];
        [_tableHeaderView addSubview:_courseLabel];

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 131.5, VIEW_WIDTH, 0.5)];
        line.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [_tableHeaderView addSubview:line];
    }
    
    return  _tableHeaderView;
}

#pragma mark -LifeCycle

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    if (_newsId) { //正常跳入
        
        [self requestData];
    }else { //阅读更多跳入
    
        [self requestByReadMore];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (_model) {
        
        _titleStr = _model.newsname;
        _editionIdStr = [NSString stringWithFormat:@"%ld期",(long)_model.editiontype];
    }
    
    chineseArr = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二"];
    
    [self.view addSubview:self.tableView];
    
    if (_editionIdStr.length > 0 && _titleStr.length > 0) {
        
        self.navBar.navigationItem.title = [NSString stringWithFormat:@"%@ %@",_editionIdStr,_titleStr];
    }
    
    
    [self.view bringSubviewToFront:self.networkErrorView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark -UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellid";
    SN_ResoQuestion_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        
        cell = [[SN_ResoQuestion_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    SN_Question_SolveList_Model *listModel = [self.dataArr objectAtIndex:indexPath.section];
    
//    SN_Question_Solve_Model *questionModel = [listModel.questionlist objectAtIndex:indexPath.row];
    cell.model = listModel;
    cell.delegate = self;
    cell.dataArr = self.dataArr;
    return cell;
}

#pragma mark -UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
 
    SN_ResoveQuestion_SectionHeaderView *view = [[SN_ResoveQuestion_SectionHeaderView alloc] init];
    
    SN_Question_SolveList_Model *model = [self.dataArr objectAtIndex:section];
    NSString *name = model.name;
    
    NSString *order = [chineseArr objectAtIndex:section];

    view.titleLabel.text = [NSString stringWithFormat:@"%@、%@",order,name];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SN_Question_SolveList_Model *model = [self.dataArr objectAtIndex:indexPath.section];
    
    if (model) {
        
        return model.cellHeight;
    }
    
    return 44;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 47;
}

#pragma mark -Request
- (void)requestData {
    
    if (!_newsId) {
        
        [self showHint:@"没有newid"];
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:@"api_szb_questionlist" forKey:@"Method"];
    [dict setObject:_newsId forKey:@"newsid"];
    [dict setObject:USERID forKey:@"userid"];
    
    __weak typeof(self) weakSelf = self;
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        NSDictionary *dict = [arr objectAtIndex:0];
        
        weakSelf.contentLabel.text = [dict objectForKey:@"newsname"];
        weakSelf.yearLabel.text = [[dict objectForKey:@"year"] stringByAppendingString:@"学年度"];
        
        NSString *course = [dict objectForKey:@"coursename"];
        NSString *phase = [dict objectForKey:@"phase"];
        NSString *newsName = [dict objectForKey:@"newsname"];
        
        NSString *courseStr = [NSString stringWithFormat:@"%@ 第%@期",course,phase];
        weakSelf.courseLabel.text = courseStr;
        
        NSString *picId = [dict objectForKey:@"pic"];
        NSString *picUrl = [NSString stringWithFormat:@"%@%@",PICPERFIXURL,picId];
        
        [weakSelf.newsImageView sd_setImageWithURL:[NSURL URLWithString:picUrl]];
        
        NSArray *questionListArr = [dict objectForKey:@"bigquestionlist"];
        self.dataArr = [SN_Question_SolveList_Model mj_objectArrayWithKeyValuesArray:questionListArr];
        
        self.navBar.navigationItem.title = [NSString stringWithFormat:@"%@期 %@",phase,newsName];
        
        [self.tableView reloadData];
        [self.networkErrorView hideEmptyImage];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
        [self.networkErrorView showEmptyImage];
    }];
}

- (void)requestByReadMore {

    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:@"api_szb_questionlistbynature" forKey:@"Method"];
    [dict setObject:@(_model.phase) forKey:@"phase"];
    [dict setObject:USERID forKey:@"userid"];
    [dict setObject:_alertModel.courseid forKey:@"courseid"];
    [dict setObject:_alertModel.edtiontype forKey:@"editiontype"];
    [dict setObject:_alertModel.gradersid forKey:@"gradersid"];
    [dict setObject:_alertModel.year forKey:@"year"];
    
    __weak typeof(self) weakSelf = self;
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        NSDictionary *dict = [arr objectAtIndex:0];
        
        weakSelf.contentLabel.text = [dict objectForKey:@"newsname"];
        weakSelf.yearLabel.text = [[dict objectForKey:@"year"] stringByAppendingString:@"学年度"];
        
        NSString *course = [dict objectForKey:@"coursename"];
        NSString *phase = [dict objectForKey:@"phase"];
        NSString *newsName = [dict objectForKey:@"newsname"];
        
        NSString *courseStr = [NSString stringWithFormat:@"%@ 第%@期",course,phase];
        weakSelf.courseLabel.text = courseStr;
        
        NSString *picId = [dict objectForKey:@"pic"];
        NSString *picUrl = [NSString stringWithFormat:@"%@%@",PICPERFIXURL,picId];
        
        [weakSelf.newsImageView sd_setImageWithURL:[NSURL URLWithString:picUrl]];
        
        NSArray *questionListArr = [dict objectForKey:@"bigquestionlist"];
        self.dataArr = [SN_Question_SolveList_Model mj_objectArrayWithKeyValuesArray:questionListArr];
        
        self.navBar.navigationItem.title = [NSString stringWithFormat:@"%@期 %@",phase,newsName];
        
        [self.tableView reloadData];
        [self.networkErrorView hideEmptyImage];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
        [self.networkErrorView showEmptyImage];
    }];

}

#pragma mark -SN_ResoQuestion_TableViewCellDelegate
- (void)clickCellButtonWithModel:(SN_Question_SolveList_Model *)model index:(NSInteger)index allModel:(NSMutableArray *)dataArr {
    
    SN_Question_Detail_Controller *vc = [[SN_Question_Detail_Controller alloc] init];
    vc.listModel = model;
    vc.selectIndex = index;
    vc.dataArr = dataArr;
    
    vc.navTitle = self.navBar.navigationItem.title;
    vc.collectionBlock = ^(){
    
        [self requestData];
    };
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:USERID forKey:@"userid"];
    [dict setObject:@"api_szb_readquestion" forKey:@"method"];
    
    SN_Question_Solve_Model *detailModel = [model.questionlist objectAtIndex:index];
    [dict setObject:detailModel.problemid forKey:@"problemid"];
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        NSDictionary *dict = [arr objectAtIndex:0];
        
        NSString *isvip = [NSString stringWithFormat:@"%@",[dict objectForKey:@"isvip"]];
        
        
        NSDictionary *imformation = [SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID];
        NSMutableDictionary *informationM = [NSMutableDictionary dictionaryWithDictionary:imformation];
        
        [informationM setObject:isvip forKey:@"isvip"];

        [SyUserInfomation personalImformationSaveWtih:informationM];
        
        


        [self.navigationController pushViewController:vc animated:YES];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
        
        
    }];
    
}

- (void)reloadCurrentData {
    
    [self requestData];
}

@end
